﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using WafApplication1.Applications.Views;
using WafApplication1.Applications.ViewModels;
using System.Waf.Applications;

namespace WafApplication1.Presentation.Views
{
    [Export(typeof(IHouseTypeView))]
    public partial class HouseTypeView : UserControl, IHouseTypeView
    {
        private Dictionary<string, string[]> cityAndCommunityDictionary = new Dictionary<string, string[]>()
        {
            { "南宁", new string[] { "南宁A社区", "南宁B社区", } },
            { "柳州", new string[] { "柳州A社区", "柳州B社区", "柳州C社区", "柳州D社区" } },
            { "桂林", new string[] { "桂林A社区", "桂林B社区", "桂林C社区" } },
        };

        public HouseTypeView()
        {
            InitializeComponent();
            // 初始化两个下拉列表
            InitComboBox();
        }


        /// <summary>
        /// 初始化选择城市的下拉列表
        /// </summary>
        private void InitComboBox()
        {
            // 初始化城市列表
            ItemCollection coll = cityComboxBox.Items;
            foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
            {
                ComboBoxItem boxItem = new ComboBoxItem() { Content = kvp.Key };
                coll.Add(boxItem);
            }

            // 给ComboBox注册一个选项改变的事件
            cityComboxBox.SelectionChanged += new SelectionChangedEventHandler(cityComboxBox_SelectionChanged);
        }

        private void cityComboxBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 当前城市的社区
            ItemCollection coll = communityComboxBox.Items;
            // 先清空
            coll.Clear();
            // 再添加
            foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
            {
                // kvp.Value = { "南宁A社区", "南宁B社区", }
                // 此时的 cityComboxBox.SelectedValue = System.Windows.Controls.ComboBoxItem: 南宁
                // 所以如果用这种方法获取选中的值，还需要切割字符串
                ComboBoxItem selectedCity = cityComboxBox.SelectedItem as ComboBoxItem;
                string cityName = selectedCity.Content.ToString();

                System.Console.WriteLine("cityName = " + cityName);
                if (cityName.Equals(kvp.Key))
                {
                    foreach (var item in kvp.Value)
                    {
                        // item = "南宁A社区"
                        ComboBoxItem boxItem = new ComboBoxItem() { Content = item };
                        coll.Add(boxItem);
                    }
                }
            }
        }

        /// <summary>
        /// 添加子页面的方法
        /// </summary>
        public void AddPageView()
        {
            HouseTypeViewModel houseTypeViewModel = ViewHelper.GetViewModel<HouseTypeViewModel>(this);
            PageView pageView = (PageView)houseTypeViewModel.PageViewModel.View;
            this.stackPanel.Children.Add(pageView);
        }
    }

}
